######################################################################
package Swignition::GenericParser::Recursive;
######################################################################

use Swignition::Misc;
use Clone qw(clone);
use strict;

sub is_enabled
{
	my $this = shift;
	return ($this->{options}->{p_rdfx} > 0);
}

sub consume
{
	my $this     = shift;
	my $thisNode = $this->model->lookup_node($this->uri);
	
	return unless $thisNode;
	
	my @links =  $thisNode->query_resource('http://www.w3.org/1999/xhtml/vocab#meta');
	push @links, $thisNode->query_resource('http://www.w3.org/2000/01/rdf-schema#seeAlso');
	push @links, $thisNode->query_resource('http://www.iana.org/assignments/relation/meta');

	foreach my $link ( @links )
	{
		next unless length $link->object->uri;
		my $uri = URI->new( $link->object->uri );
		if ($this->{NoFollow}->{ $uri->host })
		{
			$this->graph->quick_rrl(
				"$uri",
				'http://www.w3.org/2000/01/rdf-schema#comment',
				("Was not followed because of NoFollow rule for host ".$uri->host."."),
				undef,
				'en');
			next;
		}
		
		my $request  = HTTP::Request->new(GET => "$uri");
		$request->header('Referer', $this->uri);
		my $response = Swignition::Misc::get_ua->request($request);

		# Set options for a recursive parse.
		my $opts          = clone($this->{options});
		$opts->{ua}       = $this->get_ua;
		$opts->{request}  = $request;
		$opts->{response} = $response;
		$opts->{base}     = "$uri";
		$opts->{model}    = $this->model;
		$opts->{p_rdfx}--;
		
		# Recursive parse.
		my $external_page = Swignition::GenericParser::new_by_type($response->content, $opts);
		
		# If MIME type was unrecognised by new_by_type, then try as RDF/XML.
		$external_page = Swignition::RdfXmlParser->new($response->content, $opts)
			if (!$external_page);
		
		$this->graph->quick_rrl(
			"$uri",
			'http://www.w3.org/2000/01/rdf-schema#comment',
			"Could not parse this file.",
			undef,
			'en')
			if (!$external_page);
			
		# Don't really need to do anything with $external page. Everything should
		# be in the model, and in the combined graph.
	}
} #/sub consume

1;
